<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <style>
    body {
      background-color: #333;
    }
    .box {
      position: absolute;
      width: 200px;
      height: 200px;
      background-color: #f75;
    }
    button {
      margin-top: 250px;
      padding: 30px;
    }
  </style>
  <body>
    <div class="box"></div>
    <button>启动</button>
    <button>倒退</button>

    <script>
      var box = document.querySelector(".box");
      var btn = document.querySelectorAll("button");
      var timer;
      var leader;
      btn[0].onclick = function () {
        timer = setInterval(function () {
          leader = box.offsetLeft;
          box.style.left = leader + 10 + "px";

          if (1715 - box.offsetLeft < 10) {
            box.style.left = "1715px";
            clearInterval(timer);
          }
        }, 1000 / 60);
      };
      btn[1].onclick = function () {
        timer = setInterval(function () {
          leader = box.offsetLeft;
          box.style.left = leader - 10 + "px";

          if (0 - box.offsetLeft > 10) {
            box.style.left = "0px";
            clearInterval(timer);
          }
        }, 1000 / 60);
      };
    </script>
  </body>
</html>
